<template>
  <div style="padding: 10px">
    <!--    功能区域-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="addviolation" v-if="user.role === 1">新增</el-button>
    </div>


    <el-table
        :data="tableData"
        border
        stripe
        style="width: 100%">

      <el-table-column prop="id"
                       label="Id"
                       sortable/>

      <el-table-column prop="stuId"
                       label="学号"/>

      <el-table-column prop="wtime"
                       label="违纪登记时间"/>

      <el-table-column prop="wreason"
                       label="违纪内容"/>

      <el-table-column v-if="user.role===1" label="操作" width="200px">
        <template #default="scope">
          <el-button type="primary" style="margin-right: 20px" size="small" icon="el-icon-edit"
                     @click="handleEdit(scope.row)">编辑
          </el-button>

          <el-popconfirm title="确认删除此条数据吗?" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger" size="small" icon="el-icon-delete">删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>

    <!--      弹窗-->
    <el-dialog
        v-model="dialogVisible"
        :title=diatit
        width="50%"
    >
      <el-form :model="form" label-width="120px">
        <el-form-item label="违纪者学号">
          <el-input v-model="form.stuId" style="width: 80%;" ></el-input>
        </el-form-item>
        <el-form-item label="违纪内容">
          <el-input v-model="form.wreason" style="width: 80%;"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveviolation">确认</el-button>
      </span>
      </template>
    </el-dialog>


    <div>
      <!--      分页-->
      <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      >
      </el-pagination>

    </div>
  </div>
</template>

<script>

import request from "../utils/request";
import {ElMessage} from "element-plus";
// import {ElMessage} from 'element-plus'

export default {
  name: 'Home',
  components: {},
  data() {
    return {
      currentPage: 1,
      total: 0,
      search: '',
      tableData: [],
      pageSize: 10,
      diatit: '',
      dialogVisible: false,
      form: {}
    }
  },
  created() {
    let userStr = sessionStorage.getItem("user") || "{}"
    //将session取出的json数据转化为对象类型
    this.user = JSON.parse(userStr)
    this.load();
  },
  methods: {
    load() {
      request.get("/violation",
          //多个参数get方式需要添加params属性
          {
            params: {
              page: this.currentPage,
              rows: this.pageSize,
            }
          }).then(res => {
        console.log(res);
        if (res.code === '0') {
          this.tableData = res.data.rows;
          this.total = res.data.total;
        }

      })
    },

    saveviolation() {
      console.log(this.form);
      if (this.form.id) {
        //如果表单存在id,则更新
        request.put("/violation", this.form).then(res => {
          console.log(res);
          if (res.code === '0') {
            ElMessage({
              message: '更新成功!',
              type: 'success',
            })
          } else {
            ElMessage({
              message: '更新失败!',
              type: 'error'
            })
          }
          this.load();
          this.dialogVisible = false;
        })

      } else {
        //如果不存在id,则新增
        request.post("/violation", this.form).then(res => {
          console.log(res);
          if (res.code === '0') {
            ElMessage({
              message: '新增成功!',
              type: 'success',
            })
          } else {
            ElMessage({
              message: res.msg,
              type: 'error'
            })
          }
          this.load();
          this.dialogVisible = false;
        });
      }

    },

    addviolation() {
      this.diatit = '添加违纪';
      //格式化表单 form
      this.form = {};
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.diatit = '编辑公告'
      //深拷贝
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    handleDelete(id) {
      request.delete("/violation/" + id).then(res => {
        if (res.code === '0') {
          ElMessage({
            message: '删除成功!',
            type: 'success',
          })
        } else {
          ElMessage({
            message: '删除失败!',
            type: 'error'
          })
        }
        this.load();
      });
    },

    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },

  }
}
</script>
